/*
 * 文件名：SamplingTableController.java
 * 版权：Copyright by www.tiansu-china.com/
 * 描述：
 * 修改人：congriran
 * 修改时间：2020年12月2日
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
*/

package com.tiansu.reborn.wanda.modules.executor.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tiansu.reborn.wanda.modules.executor.entity.FacadeResponse;
import com.tiansu.reborn.wanda.modules.executor.entity.vo.CreateSamplingTableVO;
import com.tiansu.reborn.wanda.modules.executor.service.SamplingTableService;

import lombok.extern.slf4j.Slf4j;

@Controller
@Slf4j
public class SamplingTableController
{
    @Autowired
    private SamplingTableService samplingTableService;

    @GetMapping("/create_sampling_table")
    public String index()
    {
        return "create_sampling_table";
    }

    @PostMapping("/table_Repair")
    @ResponseBody
    public FacadeResponse tableRepair(@RequestBody CreateSamplingTableVO vo)
    {
        FacadeResponse response = null;
        if(!"TS_H1_".equalsIgnoreCase(vo.getTablePrefix()) && !"TS_DAY_".equalsIgnoreCase(vo.getTablePrefix()))
        {
            log.error("TableRepair error, Illegal parameter! parameter is" + vo);
            response = FacadeResponse.error("TableRepair error, Illegal parameter!");
        }
        else
        {
            try
            {
                int result = samplingTableService.repairSamplingTable(vo);
                String message = "成功创建了" + result + "张表！";
                response = FacadeResponse.ok(message);
            }
            catch (Exception e)
            {
                response = FacadeResponse.error(e.getMessage());
                log.error("TableRepair error, error message is: " + e.getMessage());
            }
        }
        
        return response;
    }

}
